package com.qingcloud.adminbackend.controller;


import com.google.common.collect.ImmutableMap;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.usergroup.*;
import com.qingcloud.adminbackend.service.ISysUsergroupService;
import com.qingcloud.base.annotation.LoginUser;
import com.qingcloud.base.remote.pojo.LoginUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 新用户组 前端控制器
 * </p>
 *
 * @author wangjinnan
 * @since 2018-11-15
 */
@Api(value = "用户组管理接口", description = "用户组管理接口")
@RestController
@RequestMapping("/user-group")
public class SysUsergroupController {

    @Resource
    private ISysUsergroupService sysUserGroupService;

    @ApiOperation(value = "分页查询用户组", notes = "分页查询用户组", response = SysUsergroupDTO.class)
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody SysUsergroupPageParam param, HttpServletRequest request) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.page(param, request));
    }

    @ApiOperation(value = "根据客户ID查询用户组", notes = "根据客户ID查询用户组", response = SysUsergroup.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "客户ID", dataType = "string", paramType = "path", defaultValue = "1a8f06f1590e43f5a6c84c733d05824c")
    })
    @PostMapping(value = "/org/{orgId}")
    public AjaxResult findByOrg(@PathVariable("orgId") String orgId, HttpServletRequest request) {
        return new AjaxResult(WamingConstant.SUCCESS, "",
                sysUserGroupService.listByMap(ImmutableMap.of("org_id", orgId, "delete_flag", 0)));
    }

    @ApiOperation(value = "添加用户组", notes = "添加用户组")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody SysUsergroup sysUsergroup, @LoginUser LoginUserDTO loginUserDTO) {
        sysUsergroup.setCreateBy(loginUserDTO.getUserId());
        sysUsergroup.setCreateDt(LocalDateTime.now());
        sysUsergroup.setModifyBy(loginUserDTO.getUserId());
        sysUsergroup.setModifyDt(LocalDateTime.now());
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.add(sysUsergroup));
    }

    @ApiOperation(value = "修改用户组", notes = "修改用户组")
    @PostMapping(value = "/modify")
    public AjaxResult modify(@RequestBody SysUsergroup sysUsergroup, @LoginUser LoginUserDTO loginUserDTO) {
        sysUsergroup.setModifyBy(loginUserDTO.getUserId());
        sysUsergroup.setModifyDt(LocalDateTime.now());
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.modify(sysUsergroup));
    }

    @ApiOperation(value = "查询用户组基础信息", notes = "查询用户组基础信息")
    @PostMapping(value = "/info/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户组ID", dataType = "string", paramType = "path", defaultValue = "123")
    })
    public AjaxResult queryInfo(@PathVariable("id") String id) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.queryInfo(id));
    }

    @ApiOperation(value = "根据用户组ID删除用户组", notes = "根据用户组ID删除用户组")
    @PostMapping(value = "/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户组ID，多个用,分割", dataType = "string", paramType = "query", defaultValue = "123")
    })
    public AjaxResult delete(@RequestParam("id") String id) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.deleteById(id));
    }

    @ApiOperation(value = "查看用户组下的成员", notes = "查看用户组下的成员", response = SysUsergroupUserDTO.class)
    @PostMapping(value = "/user/page")
    public AjaxResult page(@RequestBody SysUsergroupUserParam param, HttpServletRequest request) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.userPage(param));
    }

    @ApiOperation(value = "获取用户组列表", notes = "获取用户组列表", response = SysUsergroupDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "机构Id", dataType = "string", paramType = "query", required = true,
                    defaultValue = "81749b4795ed4fbb9985cf048fb0e2bb"),
            @ApiImplicitParam(name = "groupName", value = "用户组名称", dataType = "string", paramType = "query",
                    defaultValue = "")
    })
    @PostMapping(value = "/getUserGroupList")
    public AjaxResult getUserGroupList(@RequestParam("orgId") String orgId,
                                       @RequestParam(value = "groupName", required = false) String groupName) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.getUserGroupList(orgId, groupName));
    }

    /**
     * 查询指定用户所属用户组
     *
     * @return
     */
    @ApiOperation(value = "获取指定用户所属用户组列表", notes = "获取指定用户所属用户组列表", response = SysUsergroupDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "string", paramType = "query", required = true,
                    defaultValue = "81749b4795ed4fbb9985cf048fb0e2bb"),
    })
    @GetMapping(value = "/getUserGroupByUserId")
    public AjaxResult getUserGroupByUserId(@RequestParam String userId) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysUserGroupService.getUserGroupByUserId(userId));
    }

}

